/*
 * Copyright (c) 2010 CCX(China) Co.,Ltd. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of
 * CCX(China) Co.,Ltd. ("Confidential Information").
 * It may not be copied or reproduced in any manner without the express 
 * written permission of CCX(China) Co.,Ltd.
 * 
 *	Created on 2010-12-27 下午04:35:05
 */
package cn.com.ccxe.core.crawl.listing;

import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.com.ccxe.core.crawl.CrawlExpressionCache;
import cn.com.ccxe.core.db.ListPageDB;
import cn.com.ccxe.core.db.ListPageWorkQueues;
import cn.com.ccxe.core.entity.CrawlExpression;
import cn.com.ccxe.core.entity.ListPageLink;
import cn.com.ccxe.core.entity.Website;
import cn.com.ccxe.core.util.CodeUtil;
import cn.com.ccxe.dao.WebsiteDao;
import cn.com.ccxe.dao.impl.WebsiteDaoImpl;

/**
 * @author hetao
 */
public class DataUpdater extends Thread {
	
	//更新间隔
	private int 			updateInterval = 20;
	final Logger 			logger = LoggerFactory.getLogger(DataUpdater.class);
	ListPageWorkQueues 		workQueues = ListPageWorkQueues.getInstance();
	
	
	
	public void init() {
		WebsiteDao websiteDao = new WebsiteDaoImpl();
		List<Website> websiteList = websiteDao.getAllListPage();
		logger.info("添加了{}条数据至列表库！",websiteList.size());
		addListPage2DB(websiteList);
	}

	/**
	 * 往{@link ListPageDB}中添加列表页的持久化信息
	 * @param websiteList
	 */
	private void addListPage2DB(List<Website> websiteList) {
		ListPageDB listDB = ListPageDB.getInstance();
		for (Website website : websiteList) {
			//判断是否为文章列表
			if(website.getIsList() == 1) {
				ListPageLink link = constructListPage(website);
				listDB.put(link.getPath(), link);
			}
		}
		listDB.size();
	}
	
	/**
	 * 将website的信息添加到ListPageLink中。
	 * @param website
	 * @return
	 */
	private ListPageLink constructListPage(Website website) {
		ListPageLink link = new ListPageLink();
		link.setPath(website.getPath());
		link.setModuleId(website.getId());
		link.setTitle(website.getName());
		addRuleToListPage(website.getId(), link);
		return link;
	}
	
	/**
	 * 将抓取规则添加至{@link ListPageLink}中.
	 * @param websiteId 站点ID，用于查询相对应的规则。
	 * @param link
	 */
	private void addRuleToListPage(int websiteId, ListPageLink link) {
		CrawlExpression listPage = CrawlExpressionCache.get(websiteId);
		if(listPage == null ) {
			listPage = new CrawlExpression();
			logger.debug("没有找到适用于【{}】规则",link.getPath());
		}
		link.setListPage(listPage);
	}
	
	/**
	 * 更新工作队列{@link ListPageWorkQueue}.
	 * 将列表页的持久库中符合抓取条件的列表页链接添加到ListPageWorkQueue中待抓取.
	 */
	public void updateWorkQueue() {
		try {
			//FIXME 将数据库中保存的文章列表页每5分钟更新一次。而其他的将不更新 
			WebsiteDao websiteDao = new WebsiteDaoImpl();
			List<Website> websiteList = websiteDao.getAllListPage();
			logger.info("添加了{}条数据至列表库！",websiteList.size());
			addListPage2DB(websiteList);
			
			
			ListPageDB listDB;
			listDB = ListPageDB.getInstance();
			logger.info("{} 中有 {} 条数据.",listDB.getDatabaseName(),listDB.size());
			listDB.copy(workQueues,this);
			workQueues.sync();
			logger.info("{}更新后，有 {} 条数据。",workQueues.getDatabaseName(),workQueues.size());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public boolean checkUpdate(ListPageLink link) {
		int fetchTime = link.getFetchTime();
		if(fetchTime <= 0 ) {
			logger.debug("从未采集过的列表链接 -> {}",link);
			return true;
		}
		int currentTime = CodeUtil.getDate();
		int updateTime = (currentTime - fetchTime);
		if( updateTime > updateInterval) {
//			logger.debug("到达更新时间，立即更新  -> {}",link);
			return true; //FIXME 更新调度
		} else {
//			logger.debug("还差 {} 分钟更新。",(updateInterval - updateTime ));
			return false;
		}
	}
	
}
